package cn.com.customer.manager.ui.search

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import cn.com.customer.manager.R
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.CustomerReiveInfo
import cn.com.customer.manager.bean.PrivateCustomer
import cn.com.customer.manager.bean.RequestBean
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.model.ApiResult
import com.xuexiang.xui.widget.actionbar.TitleBar
import com.xuexiang.xui.widget.searchview.MaterialSearchView
import com.xuexiang.xutil.display.BarUtils
import kotlinx.android.synthetic.main.activity_search.*

class SearchActivity : BaseActivity() {

    private var params: MutableMap<String, String> = mutableMapOf()
    private var page = 1
    private var mSearchText = "";
    private var mRealSearchData: MutableList<PrivateCustomer> = mutableListOf()
    override fun getLayoutId(): Int =R.layout.activity_search

    override fun initView(savedInstanceState: Bundle?) {
        search_title.addAction(object : TitleBar.ImageAction(R.drawable.ic_action_search) {
            override fun performAction(view: View?) {
                search_view.showSearch()
            }

        })
        BarUtils.addMarginTopEqualStatusBarHeight(search_view)
        search_view.setVoiceSearch(false)
        search_view.setEllipsize(true)

        search_view.setBackgroundResource(R.color.bg_color)
        search_view.setOnQueryTextListener(object : MaterialSearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                onQueryResult(query)
                mSearchText = ""
                return false
            }

            override fun onQueryTextChange(newText: String): Boolean { //Do some magic
                mSearchText = newText
                if(mSearchText!=null){
                    requestData()
                }
                return false
            }
        })
        search_view.setOnSearchViewListener(object : MaterialSearchView.SearchViewListener {
            override fun onSearchViewShown() { //Do some magic

            }

            override fun onSearchViewClosed() {
                mSearchText = ""
            }
        })
        search_view.setSubmitOnClick(true)
    }
    private fun requestData() {
        params["pageNo"] = page.toString()
        params["pageSize"] ="1000"
        params["name"] = mSearchText
        params["telephone"] = ""
        val request = XHttp.custom()
        request.call(request.create(BaseApi.Customer::class.java)
                .customerList(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<CustomerReiveInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<CustomerReiveInfo>?) {
                        apiResult?.let {
                            if (it.data != null) {
                                mRealSearchData.clear()
                                suggestions.clear()
                                mRealSearchData.addAll(it.data.privateCustomers)
                                mRealSearchData.addAll(it.data.publicCustomers)
                                mRealSearchData.forEach {
                                    suggestions.add(it.name)
                                }
                                search_view.setSuggestions(suggestions.toTypedArray())
                            }
                        }
                    }
                })
    }
    var suggestions: MutableList<String> = mutableListOf()

    private fun onQueryResult(query: String) {
        var item=mRealSearchData[suggestions.indexOf(query)]
        setResult(Activity.RESULT_OK, Intent().putExtra("id",item.id).putExtra("name",item.name))
        finish()
    }

}
